package com.zxl.grocery.modular.schedule.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxl.grocery.modular.schedule.entity.ScheduleJobEntity;
import com.zxl.grocery.modular.schedule.mapper.ScheduleJobMapper;
import com.zxl.grocery.modular.schedule.state.ScheduleStatus;
import com.zxl.grocery.modular.schedule.utils.ScheduleUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 定时任务Service
 *
 * @author zxl
 * @Date 2017-08-23 14:20:31
 */
@Service
public class ScheduleJobMgrService extends ServiceImpl<ScheduleJobMapper, ScheduleJobEntity> {

	@Autowired
	private Scheduler scheduler;

	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init() {
		List<ScheduleJobEntity> scheduleJobList = this.baseMapper.queryList(new HashMap<>());
		for (ScheduleJobEntity scheduleJob : scheduleJobList) {
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
			//如果不存在，则创建
			if (cronTrigger == null) {
				ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
			} else {
				ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
			}
		}
	}

	/**
     * 根据ID，查询定时任务
     */
	public ScheduleJobEntity queryObject(Long jobId) {
		return this.baseMapper.queryObject(jobId);
	}

    /**
     * 查询定时任务列表
     */
	public List<ScheduleJobEntity> queryList(Map<String, Object> map) {
		return this.baseMapper.queryList(map);
	}

    /**
     * 查询总数
     */
	public int queryTotal(Map<String, Object> map) {
		return this.baseMapper.queryTotal(map);
	}

    /**
     * 保存定时任务
     */
	@Transactional
	public void saveJod(ScheduleJobEntity scheduleJob) {
		scheduleJob.setCreateTime(new Date());
		//scheduleJob.setStatus(ScheduleStatus.NORMAL.getValue());
		this.baseMapper.save(scheduleJob);

		ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
	}

    /**
     * 更新定时任务
     */
	@Transactional
	public void update(ScheduleJobEntity scheduleJob) {
		ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);

		this.baseMapper.update(scheduleJob);
	}

    /**
     * 批量删除定时任务
     */
	@Transactional
	public void deleteBatch(Long[] jobIds) {
		for (Long jobId : jobIds) {
			ScheduleUtils.deleteScheduleJob(scheduler, jobId);
		}

		//删除数据
		this.baseMapper.deleteBatch(jobIds);
	}

    /**
     * 批量更新定时任务状态
     */
	public int updateBatch(Long[] jobIds, int status) {
		Map<String, Object> map = new HashMap<>();
		List<Long> resultList = new ArrayList<>(jobIds.length);
		Collections.addAll(resultList, jobIds);
		map.put("list", resultList);
		map.put("status", status);
		return this.baseMapper.updateBatch(map);
	}

    /**
     * 立即执行
     */
	@Transactional
	public void run(Long[] jobIds) {
		for (Long jobId : jobIds) {
			ScheduleUtils.run(scheduler, queryObject(jobId));
		}
	}

    /**
     * 暂停运行
     */
	@Transactional
	public void pause(Long[] jobIds) {
		for (Long jobId : jobIds) {
			ScheduleUtils.pauseJob(scheduler, jobId);
		}

		updateBatch(jobIds, ScheduleStatus.PAUSE.getValue());
	}

    /**
     * 恢复运行
     */
	@Transactional
	public void resume(Long[] jobIds) {
		for (Long jobId : jobIds) {
			ScheduleUtils.resumeJob(scheduler, jobId);
		}

		updateBatch(jobIds, ScheduleStatus.NORMAL.getValue());
	}

	public List<Map<String, Object>> getAllScheduleJobList(String beanName) {
		return this.baseMapper.getAllScheduleJobList(beanName);
	}

	public ScheduleJobEntity selectById(Integer id) {
		return this.baseMapper.selectById(id);
	}
}
